<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>码码在线考勤系统</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
    <!-- Bootstrap 3.3.6 -->
    <link rel="stylesheet" href="/static/bootstrap/css/bootstrap.min.css">
    <!-- Font Awesome -->
    <link rel="stylesheet" href="/static/css/font-awesome.min.css">
    <!-- Ionicons -->
    <link rel="stylesheet" href="/static/css/ionicons.min.css">
    <!-- Theme style -->
    <link rel="stylesheet" href="/static/dist/css/AdminLTE.min.css">
    <link rel="stylesheet" href="/static/dist/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="/static/plugins/datatables/dataTables.bootstrap.css">
    <link rel="stylesheet" href="/static/plugins/daterangepicker/daterangepicker.css">
    <link rel="stylesheet" href="/static/plugins/datepicker/datepicker3.css">

</head>
<body class="hold-transition skin-blue sidebar-mini">
<div class="wrapper">

    <!-- Main Header -->
    <header class="main-header">

        <!-- Logo -->
        <a href="/user/home" class="logo">
            <!-- mini logo for sidebar mini 50x50 pixels -->
            <span class="logo-mini"></span>
            <!-- logo for regular state and mobile devices -->
            <span class="logo-lg">码码考勤</span>
        </a>

        <!-- Header Navbar -->
        <nav class="navbar navbar-static-top" role="navigation">
            <!-- Sidebar toggle button-->
            <a href="#" class="sidebar-toggle" data-toggle="offcanvas" role="button">
                <span class="sr-only">Toggle navigation</span>
            </a>
            <!-- Navbar Right Menu -->
            <div class="navbar-custom-menu">
                <ul class="nav navbar-nav">
                    <!-- Messages: style can be found in dropdown.less-->
                    <li class="dropdown messages-menu">
                        <!-- Menu toggle button -->
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown">
                            <i class="fa fa-envelope-o"></i>
                            <span class="label label-success">4</span>
                        </a>
                        <ul class="dropdown-menu">
                            <li class="header">您有4条信息待处理</li>
                            <li class="footer"><a href="#">查看所有信息</a></li>
                        </ul>
                    </li>
                    <!-- /.messages-menu -->
                    <!-- User Account Menu -->
                    <li class="dropdown user user-menu">
                        <!-- Menu Toggle Button -->
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown">
                            <!-- The user image in the navbar-->
                            <img src="" class="user-image user_head_img" alt="User Image">
                            <!-- hidden-xs hides the username on small devices so only the image appears. -->
                            <span class="hidden-xs user_name"></span>
                        </a>
                        <ul class="dropdown-menu">
                            <!-- Menu Footer-->
                            <li class="user-footer">
                                <div class="pull-right">
                                    <a href="#" class="btn btn-default btn-flat">退出系统</a>
                                </div>
                            </li>
                        </ul>
                    </li>

                </ul>
            </div>
        </nav>
    </header>
    <!-- Left side column. contains the logo and sidebar -->
    <aside class="main-sidebar">

        <!-- sidebar: style can be found in sidebar.less -->
        <section class="sidebar">

            <!-- Sidebar user panel (optional) -->
            <div class="user-panel">
                <div class="pull-left image">
                    <img src="" class="img-circle  user_head_img" alt="User Image">
                </div>
                <div class="pull-left info">
                    <p class="user_name"></p>
                    <!-- Status -->
                    <a href="#"><i class="fa fa-circle text-success"></i> Online</a>
                </div>
            </div>


            <!-- Sidebar Menu -->
            <ul class="sidebar-menu">

                <!-- Optionally, you can add icons to the links -->
                <li class="treeview">
                    <a href="#"><i class="fa fa-link"></i> <span>我的考勤</span>
                        <span class="pull-right-container">
              <i class="fa fa-angle-left pull-right"></i>
            </span>
                    </a>
                    <ul class="treeview-menu">
                        <li><a href="#">打卡记录</a></li>
                        <li><a href="#">审批记录</a></li>
                    </ul>
                </li>
            </ul>
            <!-- /.sidebar-menu -->
        </section>
        <!-- /.sidebar -->
    </aside>

    <!-- Content Wrapper. Contains page content -->
    <div class="content-wrapper">
        <!-- Content Header (Page header) -->
        <section class="content-header">
            <h1>
                考勤数据
            </h1>
        </section>

        <!-- Main content -->
        <section class="content">

            <div class="box">
                <div class="box-search">
                    <form onSubmit="return false;" id="attendQueryForm">
                        <div class="form-inline">
                            <label>日期范围:</label>
                            <div class="input-group">
                                <div class="input-group-addon">
                                    <i class="fa fa-calendar"></i>
                                </div>
                                <input type="text" class="form-control pull-left" id="rangeDate" name="rangeDate">
                            </div>
                            <label>状态</label>
                            <select class="form-control" name="attendStatus" id="attendStatus">
                                <option value="">全部</option>
                                <option value="1">正常</option>
                                <option value="2">异常</option>
                            </select>
                            <button type="submit" class="btn btn-primary" onclick="attendQuery()">查询</button>
                        </div>
                    </form>
                </div>
                <!-- /.box-header -->
                <!-- /.box-header -->
                <div class="box-body">
                    <table id="attend_grid" class="table table-bordered table-striped">
                        <thead>
                        <tr>
                            <th>序号</th>
                            <th>userId</th>
                            <th>考勤日期</th>
                            <th>星期</th>
                            <th>早打卡</th>
                            <th>晚打卡</th>
                            <th>缺勤时长</th>
                            <th>考勤状态</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                    </table>
                </div>
                <div id="grid_pager"></div>
                <!-- /.box-body -->
            </div>
            <!-- /.box -->

        </section>
        <!-- /.content -->
    </div>
    <!-- /.content-wrapper -->

    <!-- Main Footer -->
    <footer class="main-footer">
        <strong>Copyright &copy; 2016 <a href="#">码码在线</a>.</strong> All rights reserved.
    </footer>

</div>
<!-- ./wrapper -->

<!-- REQUIRED JS SCRIPTS -->

<!-- jQuery 2.2.3 -->
<script src="/static/jquery/jquery-2.2.3.min.js"></script>
<!-- Bootstrap 3.3.6 -->
<script src="/static/bootstrap/js/bootstrap.min.js"></script>
<!-- AdminLTE App -->
<script src="/static/dist/js/app.min.js"></script>
<script src="/static/js/user.js"></script>
<script src="/static/plugins/datatables/jquery.dataTables.min.js"></script>
<script src="/static/plugins/datatables/dataTables.bootstrap.min.js"></script>
<script src="/static/plugins/datepicker/bootstrap-datepicker.js"></script>
<script src="/static/plugins/daterangepicker/moment.min.js"></script>
<script src="/static/plugins/daterangepicker/daterangepicker.js"></script>
<script src="/static/plugins/datepicker/locales/bootstrap-datepicker.zh-CN.js"></script>
<script>
    //Date range as a button
    $('#rangeDate').daterangepicker(
            {
                locale: {
                    applyLabel: '确定',
                    cancelLabel: '取消',
                    fromLabel: '起始时间',
                    toLabel: '结束时间',
                    customRangeLabel: '自定义',
                    daysOfWeek: ['日', '一', '二', '三', '四', '五', '六'],
                    monthNames: ['一月', '二月', '三月', '四月', '五月', '六月',
                        '七月', '八月', '九月', '十月', '十一月', '十二月'],
                    firstDay: 1,
                    format:"YYYY-MM-DD",
                    separator:"/",
                },

                ranges: {
                    '今天': [moment()],
                    '昨天': [moment().subtract(1, 'days')],
                    '最近七天': [moment().subtract(6, 'days'), moment()],
                    '最近30天': [moment().subtract(29, 'days'), moment()],
                    '本月': [moment().startOf('month'), moment().endOf('month')],
                    '上月': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
                },
                startDate: moment().startOf('month'),
                endDate: moment().endOf('month')
            }
    );

    Date.prototype.format = function (fmt) {
        var o = {
            "M+": this.getMonth() + 1,                 //月份
            "d+": this.getDate(),                    //日
            "h+": this.getHours(),                   //小时
            "m+": this.getMinutes(),                 //分
            "s+": this.getSeconds(),                 //秒
            "q+": Math.floor((this.getMonth() + 3) / 3), //季度
            "S": this.getMilliseconds()             //毫秒
        };
        if (/(y+)/.test(fmt)) {
            fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
        }
        for (var k in o) {
            if (new RegExp("(" + k + ")").test(fmt)) {
                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
            }
        }
        return fmt;
    }

    function formToJson(form) {
        var result = {};
        var fieldArray = $('#' + form).serializeArray();
        for (var i = 0; i < fieldArray.length; i++) {
            var field = fieldArray[i];
            if (field.name in result) {
                result[field.name] += ',' + field.value;
            } else {
                result[field.name] = field.value;
            }
        }
        return result;
    }

    //Date picker
    $('#attendDate').datepicker({
        autoclose: true,
        language: 'zh-CN',
        todayHighlight: true
    });
    function del(data) {
        alert("删除操作");
    }
    function update(data) {
        alert("修改操作");
    }

    function detail(data) {
        alert("查看操作");
    }


    var week_CN = ["周一", "周二", "周三", "周四", "周五", "周六", "周日"]

    var table;

    function attendQuery() {
        table.ajax.reload();
    }
    $(function () {
        //提示信息
        $.fn.dataTable.ext.errMode = 'none';

        var lang = {
            "sProcessing": "处理中...",
            "sLengthMenu": "每页 _MENU_ 项",
            "sZeroRecords": "没有匹配结果",
            "sInfo": "当前显示第 _START_ 至 _END_ 项，共 _TOTAL_ 项。",
            "sInfoEmpty": "当前显示第 0 至 0 项，共 0 项",
            "sInfoFiltered": "(由 _MAX_ 项结果过滤)",
            "sInfoPostFix": "",
            "sSearch": "搜索:",
            "sUrl": "",
            "sEmptyTable": "表中数据为空",
            "sLoadingRecords": "载入中...",
            "sInfoThousands": ",",
            "oPaginate": {
                "sFirst": "首页",
                "sPrevious": "上页",
                "sNext": "下页",
                "sLast": "末页",
                "sJump": "跳转"
            },
            "oAria": {
                "sSortAscending": ": 以升序排列此列",
                "sSortDescending": ": 以降序排列此列"
            }
        };

        //初始化表格
        table = $("#attend_grid")
                .on('error.dt', function (e, settings, techNote, message) {
                    console.warn(message)
                }).dataTable({
                    language: lang, //提示信息
                    autoWidth: false, //禁用自动调整列宽
                    stripeClasses: ["odd", "even"], //为奇偶行加上样式，兼容不支持CSS伪类的场合
                    processing: true, //隐藏加载提示,自行处理
                    serverSide: true, //启用服务器端分页
                    searching: false, //禁用原生搜索
                    orderMulti: false, //启用多列排序
                    order: [], //取消默认排序查询,否则复选框一列会出现小箭头
                    renderer: "bootstrap", //渲染样式：Bootstrap和jquery-ui
                    pagingType: "simple_numbers", //分页样式：simple,simple_numbers,full,full_numbers
                    columnDefs: [{
                        "targets": 'nosort', //列的样式名
                        "orderable": false //包含上样式名‘nosort'的禁止排序
                    }],
                    ajax: function (data, callback, settings) {
                        //封装请求参数
                        var param = formToJson("attendQueryForm");
                        param.pageSize = data.length;//页面显示记录条数，在页面显示每页显示多少项的时候
                        param.start = data.start;//开始的记录序号
                        param.currentPage = (data.start / data.length) + 1;//当前页码
                        //console.log(param);
                        //ajax请求数据
                        $.ajax({
                            type: "GET",
                            url: "/attend/page",
                            cache: false, //禁用缓存
                            data: param, //传入组装的参数
                            dataType: "json",
                            success: function (result) {
                                console.log(result);
                                //console.log(result);
                                //setTimeout仅为测试延迟效果
                                setTimeout(function () {
                                    //封装返回数据
                                    var returnData = {};
                                    returnData.draw = data.startRow;//这里直接自行返回了draw计数器,应该由后台返回
                                    returnData.recordsTotal = result.totalRows;//返回数据全部记录
                                    returnData.recordsFiltered = result.totalRows;//后台不实现过滤功能，每次查询均视作全部结果
                                    returnData.data = result.items;//返回的数据列表

                                    //此时的数据需确保正确无误，异常判断应在执行此回调前自行处理完毕
                                    callback(returnData);
                                }, 200);
                            }
                        });
                    },
                    //列表表头字段
                    columns: [
                        {   "data": "seq",
                            render: function (data, type, full,row_col) {
                                return row_col.row;
                            }
                        },
                        {"data": "userId" , "visible" : false },
                        {
                            "data": "attendData",
                            render: function (data, type, full) {
                                return new Date(data).format("yyyy-MM-dd");
                            }
                        },
                        {
                            "data": "attendWeek",
                            render: function (data, type, full) {
                                return week_CN[data - 1];
                            }
                        },
                        {
                            "data": "attendMoring",
                            render: function (data, type, full) {
                                if(data!=null){
                                    return new Date(data).format("hh:mm:ss");
                                }else{
                                    return "";
                                }
                            }
                        },
                        {
                            "data": "attendEvening",
                            render: function (data, type, full) {
                                if(data!=null){
                                    return new Date(data).format("hh:mm:ss");
                                }else{
                                    return "";
                                }
                            }
                        },
                        {
                            "data": "absence"
                        },
                        {
                            "data": "attendStatus",
                            render: function (data, type, full) {
                                if(data==1){
                                    return "<span style='color: green'>正常</span>";
                                }else{
                                    return "<span style='color: red'>异常</span>";
                                }
                            }
                        }
                        ,
                        {
                            "data": "操作列",
                            render: function (data, type, full) {
                                return ('<button type="button" class="btn btn-info" onclick="detail(' + full.id + ')">详情</button> ' +
                                '<button type="button" class="btn btn-warning" onclick="update(' + full.id + ')">修改</button> ' +
                                '<button type="button" class="btn btn-danger" onclick="del(' + full.id + ')">删除</button>');
                            }
                        }
                    ]
                })
                .api();


        //此处需调用api()方法,否则返回的是JQuery对象而不是DataTables的API对象
    });

</script>
</body>
</html>
